﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.SystemUI;
using PlanningGIS.Framework;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Carto;

using ESRI.ArcGIS.Controls;
using System.Runtime.InteropServices;
using PlanningGIS.ArcGIS;

namespace PlanningGIS.Tools
{
    public partial class DropDownMapScale1 : UserControl, ICommand
    {
        IApplication m_Application = null;
        public DropDownMapScale1()
        {
            InitializeComponent();
        }

        private void cbxMapScale_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                ZoomToScale(((ComboBox)sender).Text);
            }
        }

        private void ZoomToScale(string scaleTest)
        {
            string[] scaleArray = scaleTest.Split(':');
            double scale;
            if (scaleArray.Length == 2)
            {
                double scale1;
                try
                {
                    scale1 = Convert.ToDouble(scaleArray[0]);
                    scale = Convert.ToDouble(scaleArray[1]);
                    scale = scale / scale1;
                }
                catch
                {
                    MessageBox.Show("请输入正确的比例尺信息！", "提示");
                    GetCurMapScale();
                    return;
                }
                if (scale < 0)
                {
                    MessageBox.Show("比例尺不能为负数！", "提示");
                    GetCurMapScale();
                    return;
                }
            }
            else if (scaleArray.Length == 1)
            {
                try
                {
                    scale = Convert.ToDouble(scaleArray[scaleArray.Length - 1]);
                }
                catch
                {
                    MessageBox.Show("请输入正确的比例尺信息！", "提示");
                    GetCurMapScale();
                    return;
                }
                if (scale < 0)
                {
                    MessageBox.Show("比例尺不能为负数！", "提示");
                    GetCurMapScale();
                    return;
                }
            }
            else
            {
                MessageBox.Show("请输入正确的比例尺信息！", "提示");
                GetCurMapScale();
                return;
            }

            IDisplayTransformation pDisplayTransform = (m_Application.Document as IMxDocument).ActiveView.ScreenDisplay.DisplayTransformation;
            pDisplayTransform.ScaleRatio = scale;
            (m_Application.Document as IMxDocument).ActiveView.Refresh();
        }

        public void GetCurMapScale()
        {
            if ((m_Application as IFramework).IsMapCtrlactive)
            {
                if ((m_Application.Document as IMxDocument).ActiveView.FocusMap.LayerCount > 0)
                {
                    try
                    {
                        double scale=(m_Application as IFramework).MapControl.MapScale;
                        string[] scales = scale.ToString("R").Split('.');
                        if (scale.ToString().ToUpper().Contains("E"))
                        {
                            this.cbxMapScale.Text = "1:" + scale.ToString();
                        }
                        else
                        {
                            this.cbxMapScale.Text = "1:" + (scale > 1 ? scales[0] : scale.ToString("#.######"));
                        }
                        this.cbxMapScale.Refresh();
                    }
                    catch { }
                }
            }
        }

        private void cbxMapScale_TextChanged(object sender, EventArgs e)
        {
            IMap pMap = (m_Application.Document as IMxDocument).ActiveView.FocusMap;

            List<IRasterLayer> pRasterLayers = MapHelper.GetAllLayer<IRasterLayer>(pMap);

            foreach (IRasterLayer pRasterLayer in pRasterLayers)
            {
                if (pRasterLayer.ShowResolution)
                {
                    //TOC刷新
                    ITOCControl tocControl = (ITOCControl2)(m_Application as IFramework).TOCControl;
                    tocControl.Update();
                    break;
                }
            }
            pRasterLayers.Clear();
        }

        private void cbxMapScale_DropDownClosed(object sender, EventArgs e)
        {
            ZoomToScale(this.cbxMapScale.Text);
        }
        private void cbxMapScale_SelectionChangeCommitted(object sender, EventArgs e)
        {
            ZoomToScale(this.cbxMapScale.SelectedItem.ToString());
        }

        #region ICommand Members

        int ICommand.Bitmap
        {
            get { return 0; }
        }

        string ICommand.Caption
        {
            get { return "DropDownMapScale1"; }
        }

        string ICommand.Category
        {
            get { return "比例尺"; }
        }

        bool ICommand.Checked
        {
            get { return false; }
        }

        bool ICommand.Enabled
        {
            get
            {
                return true;
            }
        }

        int ICommand.HelpContextID
        {
            get { return 0; }
        }

        string ICommand.HelpFile
        {
            get { return string.Empty; }
        }

        string ICommand.Message
        {
            get { return "DropDownMapScale1"; }
        }

        string ICommand.Name
        {
            get { return "DropDownMapScale1"; }
        }

        void ICommand.OnClick()
        {
        }

        void ICommand.OnCreate(object hook)
        {
            if (m_Application == null)
            {
                m_Application = hook as IApplication;
                IMapControlEvents2_Event pEvent = (m_Application as IFramework).MapControl as IMapControlEvents2_Event;
                pEvent.OnExtentUpdated -= new IMapControlEvents2_OnExtentUpdatedEventHandler(pEvent_OnExtentUpdated);
                pEvent.OnExtentUpdated += new IMapControlEvents2_OnExtentUpdatedEventHandler(pEvent_OnExtentUpdated);
            }
        }

        void pEvent_OnExtentUpdated(object displayTransformation, bool sizeChanged, object newEnvelope)
        {
            GetCurMapScale();
        }

        string ICommand.Tooltip
        {
            get { return "比例尺设置"; }
        }

        #endregion
    }
}
